package org.tea.inventory.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.tea.inventory.dao.tea.CustomerBigMapper;
import org.tea.inventory.domain.sys.UserInfo;
import org.tea.inventory.domain.tea.CustomerBig;
import org.tea.inventory.domain.tea.CustomerSmall;
import org.tea.inventory.service.ICustomerDistributorService;
import org.tea.inventory.utils.UserUtil;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 分销商信息Service业务层处理
 * 
 * @author dongxb
 * @date 2025-04-27
 */
@Service
public class CustomerDistributorServiceImpl implements ICustomerDistributorService
{
    @Autowired
    private CustomerBigMapper customerBigMapper;

    /**
     * 查询分销商信息
     * 
     * @param id 分销商信息主键
     * @return 分销商信息
     */
    @Override
    public CustomerBig selectCustomerBigById(Long id)
    {
        return customerBigMapper.selectCustomerBigById(id);
    }

    /**
     * 查询分销商信息列表
     * 
     * @param customerBig 分销商信息
     * @return 分销商信息
     */
    @Override
    public List<CustomerBig> selectCustomerBigList(CustomerBig customerBig)
    {
        //只查询未删除的信息
        customerBig.setDelFlag(0);
        //类型1为分销商
        customerBig.setCustomerType("1");
        return customerBigMapper.selectCustomerBigList(customerBig);
    }

    /**
     * 导出分销商信息列表
     *
     * @param customerBig 分销商信息
     * @return 分销商信息
     */
    @Override
    public List<CustomerBig> selectExport(CustomerBig customerBig) {
        List<CustomerBig> customerBigs = selectCustomerBigList(customerBig);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        customerBigs.stream().forEach(bean -> {
            bean.setStatusStr(bean.getStatus() == 0 ? "正常" : "已禁用");
            bean.setContractStr(format.format(bean.getContractStart()) + "至" + format.format(bean.getContractEnd()));
        });
        return customerBigs;
    }

    /**
     * 新增分销商信息
     * 
     * @param customerBig 分销商信息
     * @return 结果
     */
    @Override
    public int insertCustomerBig(CustomerBig customerBig)
    {
        Date date = new Date();
        UserInfo user = UserUtil.getUser();
        //客户类型为1 经销商
        customerBig.setCustomerType("1");
        customerBig.setCreateDate(date);
        customerBig.setCreateUserId(user.getLoginId());
        return customerBigMapper.insertCustomerBig(customerBig);
    }

    /**
     * 修改分销商信息
     * 
     * @param customerBig 分销商信息
     * @return 结果
     */
    @Override
    public int updateCustomerBig(CustomerBig customerBig)
    {
        Date date = new Date();
        UserInfo user = UserUtil.getUser();
        customerBig.setUpdateDate(date);
        customerBig.setUpdateUserId(user.getLoginId());
        return customerBigMapper.updateCustomerBig(customerBig);
    }

    /**
     * 批量删除分销商信息
     * 
     * @param ids 需要删除的分销商信息主键
     * @return 结果
     */
    @Override
    public int deleteCustomerBigByIds(Long[] ids)
    {
        for (Long id : ids) {
            CustomerBig customerBig = new CustomerBig();
            customerBig.setId(id);
            customerBig.setDelFlag(1);
            customerBigMapper.updateCustomerBig(customerBig);
        }
        return ids.length;
    }

    /**
     * 删除分销商信息信息
     * 
     * @param id 分销商信息主键
     * @return 结果
     */
    @Override
    public int deleteCustomerBigById(Long id)
    {
        CustomerBig customerBig = new CustomerBig();
        customerBig.setId(id);
        customerBig.setDelFlag(1);
        return customerBigMapper.updateCustomerBig(customerBig);
    }

    /**
     * 更新分销商状态
     *
     * @param id 分销商信息主键
     * @param status 分销商更新前状态
     * @return 结果
     */
    @Override
    public int updateStatus(Long id, Integer status) {
        CustomerBig customerBig = new CustomerBig();
        customerBig.setId(id);
        customerBig.setStatus(status == 0 ? 1 : 0);
        customerBig.setUpdateDate(new Date());
        //customerBig.setUpdateUserId(UserUtil.getUserInfo().getLoginId());
        return customerBigMapper.updateCustomerBig(customerBig);
    }

    @Override
    public List<CustomerBig> selectCustomerBigByCodeOrName(CustomerBig customer) {
        //类型1为分销商
        customer.setCustomerType("1");
        return customerBigMapper.selectCustomerBigByCodeOrName(customer);
    }
}
